package cn.xxstudy.blog.controller

import cn.xxstudy.blog.bean.BlogQuery
import cn.xxstudy.blog.service.BlogService
import cn.xxstudy.blog.service.CategoryService
import cn.xxstudy.blog.service.TagsService
import javassist.NotFoundException
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.crossstore.ChangeSetPersister
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.web.PageableDefault
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable

/**
 * 作者：wuxinxi on 2020/11/27
 * 包名：cn.xxstudy.blog.controller
 * TODO:标签
 */
@Controller
class TagsController {
    companion object {
        private const val BLOG_TAGS = "tags"
    }

    private val logger = LoggerFactory.getLogger(this::class.java)

    @Autowired
    private lateinit var tagsService: TagsService

    @Autowired
    private lateinit var blogService: BlogService

    @GetMapping("/$BLOG_TAGS/{id}")
    fun getTags(@PageableDefault(size = 5, sort = ["updateDateTime"], direction = Sort.Direction.DESC) pageable: Pageable,
                @PathVariable id: Long = -1, model: Model): String {
        val tagList = tagsService.listTagsTop(1000)
        if (tagList.isEmpty()) {
            throw NotFoundException("还没有任何标签哦")
        }
        var tagId = id
        if (tagId == -1L) {
            tagId = tagList[0].id
        }
        model.addAttribute("tagList", tagList)
        model.addAttribute("tagBlogListPage", blogService.listBlog(tagId, pageable))
        model.addAttribute("selectId", tagId)
        return BLOG_TAGS
    }
}